﻿Ext.define('GS.view.post.View', {
    extend: 'Ext.form.FormPanel',
    xtype: 'viewpost',
    id: 'viewpost',

    updatePost: function (postData) {
        Ext.getCmp('titleViewPost').setTitle(postData.title);
        Ext.getCmp('bodyViewPost').setHtml(postData.body);
        Ext.getCmp('viewpost').config.post_id = postData.id;
        Ext.getCmp('commentlist').getStore().load();
    },

    getPostId: function () {
        return Ext.getCmp('viewpost').config.post_id;
    },

    config: {

        post_id: -1,

        layout: {
            type: 'vbox'
        },
        scrollable: true,

        items: [
        {
            xtype: 'titlebar',
            tpl: '{title}',
            id: 'titleViewPost',
            docked: 'top',
            items:
            {
                xtype: 'button',
                text: 'Tilbage',
                ui: 'back',
                id: 'btnBackViewPost'
            }
        },
        {
            xtype: 'label',
            id: 'bodyViewPost',
            margin: 10
            //flex: 1
        },
        {
            xtype: 'button',
            text: 'Tilføj Kommentar',
            ui: 'confirm',
            id: 'btnComment',
            //flex: 2,
            height: '40px',
            margin: 10
        },
        {
            xtype: 'list',
            id: 'commentlist',
            itemTpl: '<b>{first_name} {last_name}</b> - ({date_posted:date("d/m/Y - H:i")})<p>{body}</p>',
            //flex: 3,
            scrollable: false,
            disableSelection: true,


            store: {
                model: 'GS.model.comment.Comment',

                fields: [
                    'user_id', 'body', 'date_posted, first_name, last_name'
                ],

                proxy: {
                    type: 'jsonp',
                    url: GS.util.Config.getServerUrl() + 'GetComments.php',
                    reader: {
                        type: 'json',
                        rootProperty: 'items'
                    }
                },

                autoLoad: false,

                listeners: {
                    beforeload: function (store, operation) {
                        var url = GS.util.Config.getServerUrl() + 'GetComments.php?post_id=' + Ext.getCmp('viewpost').config.post_id;
                        store.getProxy().setUrl(url);
                    }
                }
            }
        }]
    }
});